home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 235 / Issue 235 - September 2007 - DPCS0907DVD.ISO / Microsoft / Expression Blend / Blend.en.msi / Sparkle.CSwatch.Swatch.xaml.cs.en < prev    next >
Encoding:
Text File  |  2007-03-19  |  6.6 KB  |  97 lines

  1.  ■using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Data;
  7. using System.Windows.Media;
  8. using System.Windows.Media.Animation;
  9. using System.Windows.Navigation;
  10. namespace ColorSwatch
  11. {
  12.     #region Rounted Event
  13.     public class ColorSelectionEventArgs : RoutedEventArgs
  14.     {
  15.         private string color;
  16.         public string Color
  17.         {
  18.             get { return color; }
  19.             set { color = value; }
  20.         }
  21.         public ColorSelectionEventArgs(RoutedEvent id, string color)
  22.         {
  23.             base.RoutedEvent = id;
  24.             this.Color = color;
  25.         }
  26.     }
  27.     #endregion
  28.     /// <summary>
  29.     /// USER CONTROL:
  30.     /// Swatch is defined as a User Control to make it easier to send/receive events to Room.
  31.     /// It's also easier to re-use it, since each Color Swatch is an instance of the same User Control.
  32.     /// </summary>
  33.     public partial class Swatch
  34.     {
  35.         ListBox previousListBox;
  36.         // Exposes the events that can be accessed using Expression Blend, under Triggers in the Interaction panel.
  37.         public static readonly RoutedEvent OpenSwatchEvent = EventManager.RegisterRoutedEvent("OpenSwatch", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Swatch));
  38.         public static readonly RoutedEvent ColorSelectionChangedEvent = EventManager.RegisterRoutedEvent("ColorSelectionChanged", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Swatch));
  39.         public Swatch()
  40.         {
  41.             this.InitializeComponent();
  42.         }
  43.         private void OnOpenSwatch(object sender, System.Windows.Input.MouseButtonEventArgs e)
  44.         {
  45.             base.RaiseEvent(new RoutedEventArgs(Swatch.OpenSwatchEvent, this));
  46.         }
  47.         /// <summary>
  48.         /// Triggered when a new color is selected. 
  49.         /// </summary>
  50.         private void OnListBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
  51.         {
  52.             ListBox list = sender as ListBox;
  53.             if (e.AddedItems.Count != 0)
  54.             {
  55.                 // Unselects any previous item of the list boxes.
  56.                 if (previousListBox != null && previousListBox != list)
  57.                 {
  58.                     previousListBox.SelectedIndex = -1;
  59.                 }
  60.                 previousListBox = list;
  61.                 // Sends the event up to Room user control, and passes the code of the selected color in the format "#AARRGGBB".
  62.                 base.RaiseEvent(new ColorSelectionEventArgs(Swatch.ColorSelectionChangedEvent, ((System.Xml.XmlElement)e.AddedItems[0]).InnerXml));
  63.             }
  64.         }
  65.         #region Swatch Z-Index
  66.         private void BringSwatchToFront(object sender, System.Windows.Input.MouseEventArgs e)
  67.         {
  68.             FrameworkElement swatch = (FrameworkElement)sender;
  69.             Panel.SetZIndex(swatch, 1);
  70.             Storyboard bringSwatchToFrontAnimation = (Storyboard)this.Resources["toFront_" + swatch.Name];
  71.             bringSwatchToFrontAnimation.Begin(this);
  72.         }
  73.         private void SendSwatchToBack(object sender, System.Windows.Input.MouseEventArgs e)
  74.         {
  75.             FrameworkElement swatch = (FrameworkElement)sender;
  76.             Panel.SetZIndex(swatch, 0);
  77.         }
  78.         #endregion
  79.     }
  80. }